package com.aglhz.nature.presenter;

import java.util.List;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import android.util.Log;


import com.aglhz.nature.constants.ServerAPI;
import com.aglhz.nature.events.SearchViewEvent;
import com.aglhz.nature.modle.SearchBean;
import com.aglhz.nature.modle.ShopListBean;
import com.aglhz.nature.modle.item.SearchData;
import com.aglhz.nature.modle.item.ShopListData;
import com.aglhz.nature.presenter.base.BasePresenter;
import com.aglhz.nature.view.iv.SearchView;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;

public class SearchPresenter extends BasePresenter {
	private static String TAG = SearchPresenter.class.getName();
	// view
	private SearchView searchView;
	// data
	private SearchBean searchRecentBean;
	private SearchBean searchTopBean;
	private List<SearchData> data;
	private ShopListBean shopListBean;
	private List<ShopListData> mdata;

	public SearchPresenter(SearchView view) {
		super();
		searchView = view;
	}

	// /*********************data operation*******************///
	public void loadData() {
		// load remote data

//		this.loadRecentSearchBean();
		this.loadTopSearchBean();
		this.loadRecommendedSearchBean();
		// save remote data

	}

	// /*********************view operation*******************///
	private void showRecentGridView() {

		

	}

	private void showTopGridView() {
		searchView.showTopView(searchTopBean.getData());

	}


	private void showRecommedGridView(){
		searchView.showRecommedView(shopListBean.getData());
	}
	// /********************view operation*******************///
	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onEventSearchViewThread(SearchViewEvent event) {

		String msg = event.getMessage();
		Log.i(TAG, " ShopListViewEvent:" + msg);
		// update UI By event
		if (SearchViewEvent.EVENT_UPDATE_RECENT_GRIDVIEW == msg) {
			// update grid view
			this.showRecentGridView();
		} else if (SearchViewEvent.EVENT_UPDATE_TOP_GRIDVIEW == msg) {
			this.showTopGridView();
		}else if(SearchViewEvent.EVENT_UPDATE_RECOMMED_GRIDVIEW == msg){
			this.showRecommedGridView();
		}

	}

	private void loadRecentSearchBean() {
		String url = ServerAPI.APIHEARD+ServerAPI.RECENT_SEARCH;

		AsyncHttpClient client = new AsyncHttpClient();
		client.get(url, new TextHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode,
					cz.msebera.android.httpclient.Header[] headers,
					String responseString) {
				try {
					Log.e(TAG, "response" + responseString);

					Gson gson = new Gson();

					SearchBean seBean = gson.fromJson(responseString,
							SearchBean.class);
					searchRecentBean = (SearchBean) seBean.clone();

					Log.d(TAG, "View---111-----" + searchRecentBean.getOther());
					data = searchRecentBean.getData();
					Log.d(TAG, "View---mcp-----" + searchRecentBean.getData());
					// send event
					SearchViewEvent event = new SearchViewEvent(
							SearchViewEvent.EVENT_UPDATE_RECENT_GRIDVIEW);
					EventBus.getDefault().post(event);

					Log.e(TAG, "success" + "success");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(int statusCode,
					cz.msebera.android.httpclient.Header[] headers,
					String responseString, Throwable throwable) {

				Log.e(TAG, "failure");
			}

		});

	}

	private void loadTopSearchBean() {
		String url = ServerAPI.APIHEARD+ServerAPI.TOPSEARCH;

		AsyncHttpClient client = new AsyncHttpClient();
		client.get(url, new TextHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode,
					cz.msebera.android.httpclient.Header[] headers,
					String responseString) {
				try {
					Log.e(TAG, "response" + responseString);

					Gson gson = new Gson();

					SearchBean topBean = gson.fromJson(responseString,
							SearchBean.class);
					searchTopBean = (SearchBean) topBean.clone();

					Log.d(TAG,
							"searchTopBean---111-----"
									+ searchTopBean.getOther());
					data = searchRecentBean.getData();
					Log.d(TAG,
							"searchTopBean--mcp-----" + searchTopBean.getData());
					// send event
					SearchViewEvent event = new SearchViewEvent(
							SearchViewEvent.EVENT_UPDATE_TOP_GRIDVIEW);
					EventBus.getDefault().post(event);

					Log.e(TAG, "success" + "success");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(int statusCode,
					cz.msebera.android.httpclient.Header[] headers,
					String responseString, Throwable throwable) {

				Log.e(TAG, "failure");
			}

		});

	}
	public void loadRecommendedSearchBean(){
		String url = ServerAPI.APIHEARD+ServerAPI.SEARCH_RECOMMEND;

		AsyncHttpClient client = new AsyncHttpClient();
		client.get(url, new TextHttpResponseHandler() {

			@Override
			public void onSuccess(int statusCode,
					cz.msebera.android.httpclient.Header[] headers,
					String responseString) {
				try {
					Log.e(TAG, "response" + responseString);

					Gson gson = new Gson();

					ShopListBean shopBean = gson.fromJson(responseString,
							ShopListBean.class);
					shopListBean = (ShopListBean) shopBean.clone();

					Log.d(TAG,
							"ShopListBean---111-----"
									+ shopListBean.getOther());
					mdata = shopListBean.getData();
					Log.d(TAG,
							" shopListBean--mcp-----" + shopListBean.getData());
					// send event
					SearchViewEvent event = new SearchViewEvent(
							SearchViewEvent.EVENT_UPDATE_RECOMMED_GRIDVIEW);
					EventBus.getDefault().post(event);

					Log.e(TAG, "success" + "success");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(int statusCode,
					cz.msebera.android.httpclient.Header[] headers,
					String responseString, Throwable throwable) {

				Log.e(TAG, "failure");
			}

		});

	}
}
